package com.open.party.cloud.view

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import cn.android.x.model.SuperItemBean
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ArticleVo
import com.open.party.cloud.model.DictionaryBean
import com.open.party.cloud.model.SlideshowListBean
import com.open.party.cloud.view.comm.view.banner.CustomViewHolder
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.article.ArticleMainActivity
import com.open.party.cloud.view.home.article.adapter.ArticleListAdapter
import com.open.party.cloud.view.home.jiCengDangJian.OrgFor1QxListActivity
import com.open.party.cloud.view.home.ziShenJiaoYu.ZiShenJianSheLeaderListInfoActivity
import com.open.party.cloud.viewModel.ArticleViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.banner.nice.BannerConfig
import com.sinothk.banner.nice.Transformer
import kotlinx.android.synthetic.main.home_fragment_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
@Deprecated("已替换")
class HomeMainFragment : HomeMainBaseFragment(), OnSuperListener<ArticleEntity> {
    private var viewModel: ArticleViewModel? = null
    private var adapter: ArticleListAdapter? = null
    private var adapter2: ArticleListAdapter? = null
    private var currView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.home_fragment_main, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ArticleViewModel()
        initView()
    }

    private fun initView() {

        leaderItem.setOnClickListener {
            XUtils.intent().openActivity(activity, ZiShenJianSheLeaderListInfoActivity::class.java)
                .startInFragment(this)
        }
        baseOrgItem.setOnClickListener {
            XUtils.intent().openActivity(activity, OrgFor1QxListActivity::class.java).startInFragment(this)
        }

        initListView()

        xRefreshView.setEnableRefresh(true)
        xRefreshView.setEnableLoadMore(false)
        xRefreshView.setOnRefreshListener {
//            Handler().postDelayed({ initBannerData() }, 500)
            initListData()
        }
//        refreshLayout.setRefreshEnable(true)
//        refreshLayout.setLoadMoreEnable(false)
//        refreshLayout.setRefreshHandler(object : RefreshHandler {
//            override fun onLoadMore(refresh: XRefreshLayout?) {
//            }
//
//            override fun onRefresh(refresh: XRefreshLayout?) {
//                Handler().postDelayed({ initBannerData() }, 500)
//            }
//        })

        initListData()
    }

    // ==================================================================
    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    /**
     * 轮播图
     */
    private fun initBannerView(records: MutableList<ArticleEntity>) {
        val titles: ArrayList<String> = java.util.ArrayList()
        val bannerList: ArrayList<SlideshowListBean> = ArrayList()
        for (item in records) {
            titles.add(item.theme)
            bannerList.add(SlideshowListBean(item.id, item.imgUrl))
        }

        bannerView!!.setBannerTitles(titles)
            .setAutoPlay(true)
            .setPages(bannerList, CustomViewHolder())
            .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
            .setBannerAnimation(Transformer.Default)
            .setDelayTime(3000)
            .start()

        bannerView!!.setOnBannerClickListener { _, position ->
            val id = bannerList[position].id
            if (TextUtils.isEmpty(id)) {
                return@setOnBannerClickListener
            }
            XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
                .putStringExtra("id", bannerList[position].id)
                .startInFragment(this)
        }
    }

    private fun initListView() {
        adapter = ArticleListAdapter(context)
        adapter2 = ArticleListAdapter(context)

        // View 1
        recyclerView.layoutManager = object : LinearLayoutManager(activity) {
            //解决RecyclerView嵌套RecyclerView滑动卡顿的问题
            //如果你的RecyclerView是水平滑动的话可以重写canScrollHorizontally方法
            override fun canScrollVertically() = false
        }
        //解决数据加载不完的问题
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.setHasFixedSize(true)
        //解决数据加载完成后, 没有停留在顶部的问题
        recyclerView.isFocusable = false

        recyclerView.adapter = adapter
//        adapter!!.setOnSuperListener { _, itemData, _ ->
//
//        }
        adapter!!.setOnSuperListener(this)

        // View 2
        recyclerView2.layoutManager = object : LinearLayoutManager(activity) {
            //解决RecyclerView嵌套RecyclerView滑动卡顿的问题
            //如果你的RecyclerView是水平滑动的话可以重写canScrollHorizontally方法
            override fun canScrollVertically() = false
        }
        //解决数据加载不完的问题
        recyclerView2.isNestedScrollingEnabled = false
        recyclerView2.setHasFixedSize(true)
        //解决数据加载完成后, 没有停留在顶部的问题
        recyclerView2.isFocusable = false

        recyclerView2.adapter = adapter2
        adapter2!!.setOnSuperListener(this)

        // 更多
        moreInfoBtn.setOnClickListener {
            val itemList = ArrayList<SuperItemBean>()
            itemList.add(SuperItemBean("HOME_SZYW", "时政要闻"))
            gotoMore("时政要闻", itemList)
        }
        moreInfo2Btn.setOnClickListener {
            val itemList = ArrayList<SuperItemBean>()
            itemList.add(SuperItemBean("HOME_TZGG", "通知公告"))
            gotoMore("通知公告", itemList)
        }
    }

    private fun initListData() {
        // 轮播图
        initViewListData("HOME_LBXW")
        // 时政要闻
        initViewListData("HOME_SZYW")
        // 通知公告
        initViewListData("HOME_TZGG")
    }

    private fun initViewListData(type: String) {
        val pageReq = PageReq<ArticleVo>()
        pageReq.pageNo = 1
        pageReq.pageSize = 5
        val vo = ArticleVo()
        vo.childrenType = DictionaryBean(type, "")
        pageReq.data = vo

        viewModel!!.getArticleListData(pageReq, type)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun receiveStudentEventBus(result: ResultInfo<AppPageData<ArticleEntity>>) {

        if ("HOME_LBXW" == result.eventType || "HOME_SZYW" == result.eventType || "HOME_TZGG" == result.eventType) {
            xRefreshView.finishRefresh()

            when (result.code) {
                ResultInfo.SUCCESS -> {
                    if (result.data != null && result.data.records != null) {

                        when (result.eventType) {
                            "HOME_LBXW" -> {
                                initBannerView(result.data.records)
                            }
                            "HOME_SZYW" -> {
                                adapter!!.setData(result.data.records)
                            }
                            "HOME_TZGG" -> {
                                adapter2!!.setData(result.data.records)
                            }
                        }

                    } else {
                    }
                }
                ResultInfo.TOKEN_OVERDUE -> {
                    // 不存在
                }
                else -> {
                    XUtils.toast().show(result.msg)
                }
            }
        } else {
            return
        }
    }

    private fun gotoMore(title: String, itemList: ArrayList<SuperItemBean>) {
        XUtils.intent().openActivity(activity, ArticleMainActivity::class.java)
            .putStringExtra("title", title)
            .putIntExtra("index", 0)
            .putSerializableExtra("itemList", itemList)
            .startInFragment(this)
    }

    override fun onClick(position: Int, itemData: ArticleEntity, flag: String) {
        XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
            .putStringExtra("id", itemData.id)
            .startInFragment(this)
    }
}